package com.lego.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lego.bean.Goods;
import com.lego.bean.Page;
import com.lego.service.GoodsService;
import com.lego.service.impl.GoodsServiceImpl;

@WebServlet("/selectGoods.do")
public class SelectGoodsServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GoodsService gds = new GoodsServiceImpl();
		request.setCharacterEncoding("utf-8");
		String keyWord = request.getParameter("keyWord");
		String goodsType = request.getParameter("goodsType");
		String btnType = request.getParameter("btnType");
		String allGoodsType = goodsType;//判断用户是否点击了全部
		System.out.println(goodsType+"+"+allGoodsType);
		System.out.println("执行了Get后面的方法");
		String url = "";
		if(keyWord!=null&&!("").equals(keyWord)) {
			url+="&keyWord="+keyWord;
		}
		if(goodsType!=null&&!("").equals(goodsType)) {
			url+="&goodsType="+goodsType;
		}
		int pageNum = 0;
		int pageSize = 0;
		try {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		} catch (Exception e) {
			pageNum = 1;
		}
		try {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		} catch (Exception e) {
			pageSize = 16;
		}
		Page<Goods> page = gds.selcetGoods(pageNum, pageSize,keyWord,goodsType);
		System.out.println(page);
		request.setAttribute("page", page);
		request.setAttribute("url", url);
		if(keyWord!=null&&!("").equals(keyWord)) {
			request.getRequestDispatcher("selectGoods.jsp").forward(request, response);
		}else {
			if(allGoodsType!=null&&allGoodsType.equals("全部")) {
				request.getRequestDispatcher("selectGoods.jsp").forward(request, response);
				return;
			}
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GoodsService gds = new GoodsServiceImpl();
		request.setCharacterEncoding("utf-8");
		String keyWord = "";
		String goodsType = "";
		System.out.println(goodsType);
		try {
			keyWord = request.getParameter("keyWord");
		} catch (Exception e) {
			keyWord = "";
		}
		try {
			goodsType = request.getParameter("goodsType");
			if(goodsType.equals("推荐")||goodsType.equals("")||goodsType==null) {
				goodsType="";
			}
		} catch (Exception e) {
			goodsType = "";
		}
		int pageNum = 0;
		int pageSize = 0;
		try {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		} catch (Exception e) {
			pageNum = 1;
		}
		try {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		} catch (Exception e) {
			pageSize = 12;
		}
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper om = new ObjectMapper();
		Page<Goods> page = gds.selcetGoods(pageNum, pageSize,keyWord,goodsType);
		System.out.println(page);
		String str = om.writeValueAsString(page);
		response.getWriter().write(str);
	}
}
